package com.caipiao.lottery.core;

import com.caipiao.lottery.cache.MemCacheManager;
import com.caipiao.lottery.dbwarpper.P_API_USER;
import com.caipiao.lottery.task.SystemTaskManager;
import com.caipiao.lottery.config.Config;
import com.caipiao.lottery.config.ConfigAutoLoader;
import com.caipiao.lottery.status.StatusHttpServer;
import com.caipiao.lottery.log.TraceLogger;
//import com.caipiao.lottery.lty.payhttp.PayHttpServer;
import com.caipiao.lottery.lty.server.LtyHttpServer;
import com.caipiao.util.cache.RemoteCache;
import com.caipiao.util.db.DBHelper;

//单态
public class SYSInterface
{	
	private static SYSInterface instance = null;
	
	private LtyHttpServer ltyServer = null;
//	private PayHttpServer payServer = null;

	private StatusHttpServer statusServer = null;
	
	public boolean model_ltysrv = false;
	public boolean model_yptsrv = false;
	public boolean model_task = false;
	public boolean model_qmfsrv = false;
	
	public String App_ID = "";
	
	private SYSInterface()
	{
	}
	
	public static SYSInterface Instance()
	{
		if (null == instance)
		{
			instance = new SYSInterface();
		}
		
		return instance;
	}

	public boolean Init(String cfgFile)
	{
		//配置文件加载
		try
		{
    		if (!Config.Start.Load(cfgFile))
    		{
    			//日志，基本参数加载失败
    			throw new Exception("StartConfig启动文件加载失败:" + cfgFile);
    		}
    		
    		TraceLogger.System.info("StartConfig启动文件加载成功");
    		
    		//模块
    		String model = Config.Start.GetPropValue("Model");
    		this.model_ltysrv = (model.indexOf("|LTYSRV|") >= 0);
    		this.model_qmfsrv = (model.indexOf("|QMFSRV|") >= 0);
    		this.model_task = (model.indexOf("|TASK|") >= 0);
    		
    		this.App_ID = Config.Start.GetPropValue("AppId");
    		
    		//配置初始化
    		return ConfigAutoLoader.LoadConfig();
		}
		catch(Throwable e)
		{
			TraceLogger.System.error("系统启动配置初始化异常", e);
			return false;
		}
	}
	
	public boolean Start()
	{
		try
		{
			ConfigAutoLoader.Start();
			TraceLogger.System.info("配置文件自动重载线程启动成功");
			
			//固定配置初始化
			SYSConstant.Init();

    		//初始化数据库
    		if (!DBHelper.Instance().initDB(Config.DB.GetProps()))	//失败
    		{
    			//日志，数据库初始化失败
    			throw new Exception("DBHelper数据库初始化失败");
    		}
    		
    		TraceLogger.System.info("DBHelper数据库连接池初始化成功");
			
    		if (Config.Init.GetPropValue("StartTestDBPool").equalsIgnoreCase("1"))
    		{
        		//数据库连接测试
        		if (!DBHelper.Instance().DBLinkTest())
        		{
        			throw new Exception("DBHelper数据库连接池测试异常");
        		}
        		
        		TraceLogger.System.info("DBHelper数据库连接池测试成功");
    		}
    		

    		if (this.model_task)
    		{
    			try
    			{
    				SystemTaskManager.Instance().Init(Config.Task.GetProps());
    			}
    			catch(Exception e)
            	{
            		throw new Exception("异步任务服务启动失败", e);
            	}

            	TraceLogger.System.info("异步任务服务启动成功");
    		}

    		//启动服务
    		if (this.model_ltysrv)
    		{
            	try
            	{
                	//服务
                	if (null == ltyServer)
                	{
                		String HttpMode = Config.Init.GetPropValue("SYS.LtyServerMode", this.App_ID);
                		int port = Integer.valueOf(Config.Init.GetPropValue("SYS.LtyServerPort", this.App_ID));
                		String context = Config.Init.GetPropValue("SYS.LtyServerContext", this.App_ID);
                		
                		ltyServer = new LtyHttpServer(HttpMode, port, context);
                	}
                	
                	//启动服务端
                	ltyServer.startServer();
                	
            	}
            	catch(Exception e)
            	{
            		throw new Exception("LtyHttpServer监听服务启动失败", e);
            	}

            	TraceLogger.System.info("LtyHttpServer监听服务启动成功");
    		}
    		
    		//启动服务
    		if (this.model_qmfsrv)
    		{
    			try
            	{
//                	//服务
//                	if (null == payServer)
//                	{
//                		String HttpMode = Config.Product.GetPropValue("Pay.PayServerMode", this.App_ID);
//                		int port = Integer.valueOf(Config.Product.GetPropValue("Pay.PayServerPort", this.App_ID));
//                		String context = Config.Product.GetPropValue("Pay.PayServerContext", this.App_ID);
//
////                		payServer = new PayHttpServer(HttpMode, port, context);
//                	}
                	
                	//启动服务端
//                	payServer.startServer();

            	}
            	catch(Exception e)
            	{
            		throw new Exception("PayHttpServer监听服务启动失败", e);
            	}

            	TraceLogger.System.info("PayHttpServer监听服务启动成功");
    		}
    		

    		//状态服务
    		int statusPort = Integer.valueOf(Config.Init.GetPropValue("SYS.StatusServerPort", this.App_ID));
    		statusServer = new StatusHttpServer(statusPort);
    		statusServer.startServer();

			TraceLogger.System.info("StatusHttpServer监听服务启动成功");

			//初始cookie状态
			boolean db_r = P_API_USER.initCookieUserId("100101");
			if (!db_r) {
				TraceLogger.System.info("Initting Cookie DB Fiald");
				return false;
			}
			TraceLogger.System.info("Initting Cookie DB SUCCESS !!");

			//初始user在线状态
			db_r = P_API_USER.initUserOnline("100101");
			if (!db_r) {
				TraceLogger.System.info("Initting UserOnline DB Fiald");
				return false;
			}
			TraceLogger.System.info("Initting UserOnline DB SUCCESS !!");


			//初始化memcache
			MemCacheManager.Instance().CleanCache();
			TraceLogger.System.info("Init MemCache SUCCESS!!");
    			
    		return true;
		}
		catch(Throwable e)
		{
			TraceLogger.System.error("系统启动失败", e);
			return false;
		}
	}
	
	public void stop()
	{
	}
}
